package com.yao.club.controller;

import com.netease.yidun.sdk.core.utils.StringUtils;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.entity.vo.PostsInfoVO;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.request.PostsCreateRequestEntity;
import com.yao.club.service.PostsInfoService;
import com.yao.club.token.PassToken;
import com.yao.club.token.UserToken;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/postsInfo")
@Api(value = "俱乐部帖子-帖子模块", description = "俱乐部帖子-帖子模块", tags = "俱乐部帖子-帖子模块")
public class PostsInfoController {

    @Autowired
    PostsInfoService postsInfoService;
    @Resource
    HttpServletRequest request;

    

    //https://iovapicyclone.zonsenmotor.com/club-moto/app/postsInfo/giveValidationLike?postsId=1905528400386129921
    @PassToken
    @ApiOperation(value = "校验帖子", notes = "校验帖子", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id", required = true)
    })
    @GetMapping("/giveValidationLike")
    public ResponseEntity<Boolean> giveValidationLike(String postsId){
        if (StringUtils.isEmpty(postsId)) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        return postsInfoService.giveValidationLike(postsId);
    }

    //https://iovapicyclone.zonsenmotor.com/club-moto/app/postsInfo/getByPostsId?postsId=1905528400386129921
    @PassToken
    @ApiOperation(value = "帖子详情", notes = "帖子详情", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id:1905528400386129921", required = true)
    })
    @GetMapping("/getByPostsId")
    public ResponseEntity<PostsInfoVO> getByPostsId(String postsId){
        if (StringUtils.isEmpty(postsId)) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        return postsInfoService.getByPostsId(postsId);
    }


    @UserToken
    @ApiOperation(value = "点赞/取消点赞帖子", notes = "点赞/取消点赞帖子", httpMethod = "GET")
    @GetMapping("/giveLike")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id:1905528400386129921", required = true)
    })
    public ResponseEntity<Boolean> giveLike(String postsId){
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(postsId)) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        return postsInfoService.giveLike(Long.parseLong(postsId),username);
    }

    @UserToken
    @ApiOperation(value = "收藏/取消收藏帖子", notes = "收藏/取消收藏帖子", httpMethod = "GET")
    @GetMapping("/giveCollect")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id:1905528400386129921", required = true)
    })
    public ResponseEntity<Boolean> giveCollect(String postsId){
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(postsId)) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        return postsInfoService.giveCollect(Long.parseLong(postsId),username);
    }

    @UserToken
    @ApiOperation(value = "分享帖子", notes = "分享帖子", httpMethod = "GET")
    @GetMapping("/giveShare")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id:1905528400386129921", required = true),
            @ApiImplicitParam(dataType = "String", name = "shareType", value = "分享类型:qq,wechat,weibo", required = true)
    })
    public ResponseEntity<Boolean> giveShare(String postsId,String shareType){
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(postsId)) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        return postsInfoService.giveShare(Long.parseLong(postsId),username,shareType);
    }

    @UserToken
    @ApiOperation(value = "屏蔽帖子", notes = "屏蔽帖子", httpMethod = "GET")
    @GetMapping("/shield")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(dataType = "String", name = "postsId", value = "帖子id:1905528400386129921", required = true)
    })
    public ResponseEntity<Boolean> shield(String postsId){
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(postsId)) {
            return ResponseUtils.failed("帖子ID不能为空");
        }
        return postsInfoService.shield(Long.parseLong(postsId),username);
    }

    @PassToken
    @ApiOperation(value = "分页查询帖子列表-各种条件", notes = "支持登录和未登录状态查询", httpMethod = "GET")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "isFocus", value = "是否关注,默认为0", required = false),
            @ApiImplicitParam(name = "memberId", value = "用户id", required = false),
            @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = false),
            @ApiImplicitParam(dataType = "String", name = "topicId", value = "话题ID", required = false),
            @ApiImplicitParam(dataType = "Integer", name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(dataType = "Integer", name = "pageSize", value = "每页数量", required = true),
            @ApiImplicitParam(dataType = "String", name = "sortType", value = "排序类型：new-最新，hot-最热", required = false)
    })
    @GetMapping("/pageList")
    public ResponseEntity<ResultEntity<PostsInfoVO>> pageList(
            @RequestParam(required = false,defaultValue = "0") Integer isFocus,
            @RequestParam(required = false) String memberId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String topicId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String sortType) {

        String username = (String) request.getAttribute("username");
        
        if (pageNum < 1) {
            return ResponseUtils.failed("页码不能小于1");
        }
        if (pageSize < 1 || pageSize > 50) {
            return ResponseUtils.failed("每页数量必须在1-50之间");
        }
        return postsInfoService.pageList(topicId,keyword, pageNum, pageSize, sortType,memberId,isFocus, username);
    }

    @UserToken
    @ApiOperation(value = "发布帖子", notes = "发布纯文本帖子，支持话题和定位")
    @PostMapping("/create")
    public ResponseEntity<Boolean> createPost(@RequestBody PostsCreateRequestEntity entity) {
        if (StringUtils.isEmpty(entity.getContent())) {
            return ResponseUtils.failed("帖子内容不能为空");
        }

        String username = (String) request.getAttribute("username");
        return postsInfoService.createPost(entity, username);
    }

}
